home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / SPMATE12.ZIP / SPELCHEK.PA$ / spelchek.pas
Pascal/Delphi Source File  |  1993-09-20  |  6KB  |  239 lines

  1. {***************************************************}
  2. {                                                   }
  3. {   SpelChek.Pas                                        }
  4. {                                                   }
  5. {   Copyright (c) 1993 by Aciran Software Systems   }
  6. {                                                   }
  7. {***************************************************}
  8.  
  9.  
  10. program SpelChek;
  11.  
  12.  
  13. uses WinTypes,
  14.      WinProcs,
  15.      WinDos,
  16.      Strings,
  17.      Objects,
  18.      OWindows,
  19.      ODialogs,
  20.      OStdDlgs,
  21.      CommDlg,
  22.      Spell; {Use am Import Unit to Load DLL on startup}
  23.  
  24. {$R spelfile}
  25.  
  26. const
  27.   MaxWordLen = 20;
  28.  
  29. { Resource IDs }
  30.  
  31.   id_Menu       = 100;
  32.   id_About      = 100;
  33.  
  34.  
  35. { Menu command IDs }
  36.  
  37.   cm_FileOpen   = 101;
  38.   cm_HelpAbout  = 106;
  39.  
  40. { Other Constants }
  41.  
  42.  
  43. { Filename string }
  44.   type
  45.   TFileName = array [0..255] of char;
  46.  
  47.  
  48. { Application main window }
  49.  
  50.   type
  51.  
  52.   PScanTextWindow = ^TScanTextWindow;
  53.   TScanTextWindow = Object(TWindow)
  54.  
  55.     FileName : TFileName;
  56.  
  57.     constructor Init(AParent: PWindowsObject; AName: PChar);
  58.     destructor  Done; virtual;
  59.  
  60.     procedure SetupWindow; virtual;
  61.  
  62.     procedure CMFileOpen(var Msg: TMessage);
  63.       virtual cm_First + cm_FileOpen;
  64.     procedure CMHelpAbout(var Msg: TMessage);
  65.       virtual cm_First + cm_HelpAbout;
  66.     procedure ScanTextFile(AFileName:Pchar);
  67.     function GetWord(S: PChar; var F : Text): PChar;
  68.  end;
  69.  
  70. { Application object }
  71. type
  72.   TScanTextApp = Object(TApplication)
  73.   procedure InitMainWindow; virtual;
  74. end;
  75.  
  76. { Initialized globals }
  77.  
  78. const
  79.   ProgramTitle: PChar = 'Text File Spell Checker for Windows';
  80.  
  81. { Global variables }
  82.  
  83. var
  84.   App: TScanTextApp;
  85.   WordRead: array[0..MaxWordLen] of Char;
  86.   Abort : Boolean;
  87.  
  88.  
  89.  
  90. { TScanTextWindow Methods }
  91.  
  92. constructor TScanTextWindow.Init(AParent: PWindowsObject; AName: PChar);
  93. var result :integer;
  94. begin
  95.   TWindow.Init(AParent, AName);
  96.   Attr.Menu:= LoadMenu(HInstance, PChar(id_Menu));
  97.   StrCopy(FileName, '');
  98.   result := SpelmateInit;
  99.   if result >=0 then
  100.   begin
  101.     MessageBox(HWindow,'Spell Checker Failed to load successfully','Application error',MB_OK);
  102.     Halt;
  103.   end;
  104. end;
  105.  
  106. destructor TScanTextWindow.Done;
  107. begin
  108.   TWindow.Done;
  109.  end;
  110.  
  111. procedure TScanTextWindow.SetUpWindow;
  112. begin
  113.   TWindow.SetupWindow;
  114. end;
  115.  
  116. { Displays the "Open File Dialog" from Common dialogs and permit the user
  117.   to select from among the available ascii files.
  118. }
  119. procedure TScanTextWindow.CMFileOpen(var Msg: TMessage);
  120. const
  121.   DefExt = 'txt';
  122. var
  123.   OpenFN      : TOpenFileName;
  124.   Filter      : array [0..100] of Char;
  125.   FullFileName: TFilename;
  126. begin
  127.   StrCopy(FullFileName, '');
  128.  
  129. { Set up a filter buffer to look for Doc & Text files only.  Recall that filter
  130.   buffer is a set of string pairs, with the last one terminated by a
  131.   double-null.
  132. }
  133.   FillChar(Filter, SizeOf(Filter), #0);  { Set up for double null at end }
  134.   StrCopy(Filter, 'ASCII Files');
  135.   StrCopy(@Filter[StrLen(Filter)+1], '*.doc;*.txt');
  136.  
  137.   FillChar(OpenFN, SizeOf(TOpenFileName), #0);
  138.   with OpenFN do
  139.   begin
  140.     hInstance     := HInstance;
  141.     hwndOwner     := HWindow;
  142.     lpstrDefExt   := DefExt;
  143.     lpstrFile     := FullFileName;
  144.     lpstrFilter   := Filter;
  145.     lpstrFileTitle:= FileName;
  146.     flags         := ofn_FileMustExist;
  147.     lStructSize   := sizeof(TOpenFileName);
  148.     nFilterIndex  := 1;       {Index into Filter String in lpstrFilter}
  149.     nMaxFile      := SizeOf(FullFileName);
  150.   end;
  151.   if GetOpenFileName(OpenFN) then
  152.     ScanTextFile(FullFileName);
  153. end;
  154.  
  155. { Displays the program's About Box dialog.
  156. }
  157. procedure TScanTextWindow.CMHelpAbout(var Msg: TMessage);
  158. begin
  159.   Application^.ExecDialog(New(PDialog, Init(@Self, 'About')));
  160. end;
  161.  
  162.  
  163. { TScanTextApp Methods }
  164.  
  165. procedure TScanTextApp.InitMainWindow;
  166. begin
  167.   MainWindow := New(PScanTextWindow, Init(nil, 'Text File Spell Checker for Windows'));
  168. end;
  169.  
  170. { Given an open text file, read it and return the next word }
  171.  
  172. function TScanTextWindow.GetWord(S: PChar; var F : Text): PChar;
  173. var
  174.   C : Char;
  175.   I: Integer;
  176. begin
  177.   I := 0;
  178.   C := #0;
  179.   { find first letter }
  180.   while not Eof(F) and not (UpCase(C) in ['A'..'Z','''']) do
  181.     Read(F, C);
  182.   { special test in case end of file }
  183.   if Eof(F) and (UpCase(C) in ['A'..'Z','''']) then
  184.   begin
  185.     if (I < MaxWordLen) then S[I] := C;
  186.   end
  187.   else
  188.     { read chars from file, append to S }
  189.     while (UpCase(C) in ['A'..'Z','''']) and not Eof(F) do
  190.     begin
  191.       if I < MaxWordLen then
  192.       begin
  193.         S[I] := C;
  194.         Inc(I);
  195.       end;
  196.       Read(F, C);
  197.     end;
  198.   S[I] := #0;
  199.   if I >= 1 then
  200.   if S[I-1] = '''' then {get rid of trailing apostrophies}
  201.   S[I-1] := #0;
  202.   if S[0] = '''' then {and leading ones}
  203.   Move(S,S[1],Strlen(S) -1);
  204.   GetWord := S;
  205. end;
  206.  
  207. procedure TScanTextWindow.ScanTextFile(AFileName:Pchar);
  208. var
  209.  WordFile:Text;
  210.  result:PChar;
  211. begin
  212.  Abort := false;
  213.  Assign(WordFile,AFileName);        {Assign Handle to External file}
  214.  ReSet(WordFile);                              {Open for Read Only}
  215.   repeat
  216.     if GetWord(WordRead, WordFile)^ <> #0 then
  217.       if not Spellcheck(WordRead) then
  218.       begin
  219.         MessageBeep(0);
  220.         result := suggestword(WordRead);
  221.         if result[0] <> #0 then
  222.         MessageBox(HWindow,result,'Word Selected',MB_OK)
  223.       else
  224.         Abort := true;
  225.       end;
  226.   until (WordRead[0] = #0) or Abort;
  227.   Close(WordFile);
  228.   MessageBeep(0);
  229.   MessageBox(0,'Spell Check Complete','All Done',MB_OK);
  230. end;
  231.  
  232. { Main program }
  233.  
  234. begin
  235.   App.Init(ProgramTitle);
  236.   App.Run;
  237.   App.Done;
  238. end.
  239.